import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Eat extends StatefulWidget {
  const Eat({super.key});

  @override
  State<Eat> createState() => _EatState();
}

class _EatState extends State<Eat> {
  int canteenNember = Random().nextInt(2) + 1;
  int canteenFloor = Random().nextInt(2) + 1;
  late int canteenWindow;
  late int canteenW;
  num random = Random().nextInt(100) + 1;

  List<Widget> items = [];

  final _Controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    double scrW = MediaQuery.of(context).size.width;

    return Scaffold(
        body: Container(
      width: scrW,
      color: Colors.white,
      child: Column(
        children: [
          const SizedBox(
            height: 200,
          ),
          Container(
              margin: EdgeInsets.symmetric(horizontal: 50),
              padding: EdgeInsets.symmetric(horizontal: 20),
              decoration: BoxDecoration(
                  border: Border.all(width: .5),
                  borderRadius: BorderRadius.circular(40)),
              child: Center(
                child: TextField(
                  textAlign: TextAlign.center,
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(2),
                  ],
                  controller: _Controller,
                  cursorColor: Colors.black,
                  decoration: const InputDecoration(
                    border: InputBorder.none,
                    hintText: '窗口数',
                  ),
                ),
              )),
          const SizedBox(
            height: 20,
          ),
          SizedBox(
            width: 200,
            height: 80,
            child: ElevatedButton(
              onPressed: () {
                if (int.tryParse(_Controller.text) != null) {
                  canteenW = int.parse(_Controller.text);
                  canteenWindow = Random().nextInt(canteenW) + 1;
                } else {
                  showDialog(
                      context: context,
                      builder: (BuildContext context) {
                        return AlertDialog(
                          title: const Text('提示'),
                          content: const Text(
                            '请输入窗口数量',
                            style: TextStyle(fontSize: 18),
                          ),
                          actions: [
                            TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: const Text('关闭'))
                          ],
                        );
                      });
                  return;
                }
                String text = big(canteenNember) + '食堂$canteenFloor楼第$canteenWindow号窗口';
                String waimai = '吃外卖';
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      title: const Text('结果'),
                      content: Text(
                        random > 10
                            ? '今天吃' + text
                            : waimai,
                        style: TextStyle(fontSize: 18),
                      ),
                      actions: <Widget>[
                        TextButton(
                          onPressed: () {
                            Navigator.of(context).pop(); // 关闭对话框
                          },
                          child: Text('关闭'),
                        ),
                        TextButton(
                          onPressed: () {
                            setState(() {
                              canteenNember = Random().nextInt(2) + 1;
                              canteenFloor = Random().nextInt(2) + 1;
                              canteenWindow = Random().nextInt(canteenW) + 1;
                              Future.delayed(Duration(milliseconds: 1),(){
                                random = Random().nextInt(100) + 1;
                              });
                              items.add(
                                Container(
                                  margin: EdgeInsets.all(10),
                                  child: Text( random > 10
                                      ? text
                                      : waimai,style: TextStyle(fontSize: 20),),
                                ),
                              );

                            });
                            Navigator.of(context).pop();
                          },
                          child: Text('重来'),
                        ),
                      ],
                    );
                  },
                );
                setState(() {
                  canteenNember = Random().nextInt(2) + 1;
                });
              },
              child: Text(
                '今天吃什么',
                style: TextStyle(color: Colors.black, fontSize: 20),
              ),
            ),
          ),
          Expanded(child: SingleChildScrollView(
            child: Column(
              children: items,
            ),
          ))
        ],
      ),
    ));
  }
}

big(num) {
  if (num == 1) {
    return '一';
  }
  if (num == 2) {
    return '二';
  }
}
